package com.syscom.qcRule.util;

import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

import com.syscom.safe.util.xml.XmlAggregate;
import com.syscom.safe.util.xml.XmlElement;
import com.syscom.safe.util.xml.XmlException;
import com.syscom.safe.util.xml.XmlParser;

/**
 * @author Jean
 */
public class GetQCRulePathUtil {
	@SuppressWarnings("unused")
	private transient static final String mark = "10BCC85DDC54DDF27A18E78A2AA7EB50A27C9789E7CDBE9FD4DD9C9DC56D7247A585625B8AE77885";
	private static Logger mLogger = Logger.getLogger(GetQCRulePathUtil.class.getName());
	private static GetQCRulePathUtil instance = new GetQCRulePathUtil();
	
	private static final String sFilePath = "../config/RulePath-Config.xml";
	private static final String RULE_ID = "id";
	private static final String RULE_PATH = "path";
	
	private HashMap<Integer, String> mMapRulePath = new HashMap<Integer, String> ();
	
	public static GetQCRulePathUtil getInstance() { return instance; }
	
	private GetQCRulePathUtil() {
		XmlAggregate agrRoot = loadXmlAggr(sFilePath);
		parseRulePath(agrRoot);
	}

	private XmlAggregate loadXmlAggr(String sFilePath) {
		XmlAggregate agrRoot = null;
		try {
			agrRoot = XmlParser.parseXmlFile(sFilePath);
		}
		catch (XmlException e) {
			if (mLogger.isEnabledFor(Level.ERROR)) {
				mLogger.error(e.getMessage(), e);
			}
		}
		catch (FileNotFoundException e) {
			if (mLogger.isEnabledFor(Level.ERROR)) {
				mLogger.error(e.getMessage(), e);
			}
		}
		
		return agrRoot;
	}
	
	private void parseRulePath(XmlAggregate agrRoot) {
		List<XmlElement> list = agrRoot.getElements();
		
		for (XmlElement element : list) {
			String sRuleId = element.getAttribValue(RULE_ID);
			String sRulePath = element.getAttribValue(RULE_PATH);
			
			int iRuleId = Integer.parseInt(sRuleId);
			mMapRulePath.put(iRuleId, sRulePath);
		}
	}
	
	public String getRulePath(int iRuleId) {
		String sRulePath = "";
		if (mMapRulePath.containsKey(iRuleId)) {
			sRulePath = mMapRulePath.get(iRuleId);
		}
		return sRulePath;
	}
	
}
